﻿using Common.MyCustomException;
using Common.Result;
using IService;
using Model.Entitys;
using SqlSugar;
using System.Xml.Linq;


namespace Service
{
    public class CategoryService : ICategoryService
    {
        private readonly ISqlSugarClient _db;
        public CategoryService(ISqlSugarClient db)
        {
            _db = db;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="category"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> SaveCategoryAsync(Category category, long id)
        {
            category.UpdateUser = id;
            category.CreateUser = id;
            var res = await _db.Insertable(category).ExecuteReturnSnowflakeIdAsync();
            if (res > 0) return true;
            return false;
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="MyCustomException"></exception>
        public async Task<bool> DeleteCategoryByIdAsync(long id)
        {
            //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
            var dishCount=await _db.Queryable<Dish>().Where(it=>it.CategoryId==id).CountAsync();
            if (dishCount > 0) throw new MyCustomException("当前分类下关联了菜品，不能删除");
            //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
            var setmealCount =await _db.Queryable<Setmeal>().Where(it => it.CategoryId == id).CountAsync();
            if (setmealCount > 0) throw new MyCustomException("当前分类下关联了套餐，不能删除");
            //正常删除分类
            return await _db.Deleteable<Category>()
                .Where(it => it.Id == id)
                .ExecuteCommandAsync() > 0;
        }

        public async Task<bool> EditCategoryAsync(Category category, long id)
        {
            category.UpdateUser = id;
            //更新忽略null并且忽略默认值(比如int默认值是0就不更新)
            return await _db.Updateable(category).IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
                .Where(it => it.Id == category.Id).ExecuteCommandAsync() > 0;
        }

        public async Task<Category> GetCategoryByIdAsync(long id)
        {
            return await _db.Queryable<Category>().FirstAsync(it => it.Id == id);
        }

        /// <summary>
        /// 分页查询，根据sort排序
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<PageInfo> GetCategoryPageAsync(int page, int pageSize, string? name)
        {
            RefAsync<int> total = 0;
            var categoryList = await _db.Queryable<Category>()
                .WhereIF(!string.IsNullOrEmpty(name), it => it.Name.Contains(name))
                .OrderBy(it => it.Sort)
                .ToPageListAsync(page, pageSize, total);
            return new PageInfo()
            {
                Total = total,
                Data = categoryList
            };
        }

        /// <summary>
        /// 根据type查询
        /// <returns></returns>
        public async Task<List<Category>> GetCategorysByTypeAsync(int type)
        {
            return await _db.Queryable<Category>()
                .OrderBy(it=>it.Sort)
                .OrderBy(it=>it.UpdateTime, OrderByType.Desc)
                .WhereIF(type!=0,it => it.Type== type).ToListAsync();
        }
    }
}
